package com.xyazm.order.statemachine.action.order.create.node;

import com.xyazm.common.enums.OrderTypeEnum;
import com.xyazm.order.dao.OrderInfoDAO;
import com.xyazm.order.domain.param.CreateOrderParam;
import com.xyazm.order.enums.*;
import com.xyazm.order.exception.OrderBizException;
import com.xyazm.order.exception.OrderErrorCodeEnum;
import com.xyazm.process.engine.process.ProcessContext;
import com.xyazm.process.engine.process.StandardProcessor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * 创建订单 检查参数节点
 * @author xuyou
 * @date 2023年02月01日 21:59
 */
@Component
public class CreateOrderCheckNode extends StandardProcessor {

    @Autowired
    private OrderInfoDAO orderInfoDAO;

    @Override
    protected void processInternal(ProcessContext context) {
        CreateOrderParam param = context.get("createOrderParam");
        // 校验业务线标识
        if (BusinessIdentifierEnum.getByCode(param.getBusinessIdentifier()) == null) {
            throw new OrderBizException(OrderErrorCodeEnum.BUSINESS_IDENTIFIER_ERROR);
        }
        // 校验订单类型
        if (OrderTypeEnum.getByCode(param.getOrderType()) == null) {
            throw new OrderBizException(OrderErrorCodeEnum.ORDER_TYPE_ERROR);
        }
        // 校验配送类型
        if (DeliveryTypeEnum.getByCode(param.getOrderAddressParam().getDeliveryType()) == null) {
            throw new OrderBizException(OrderErrorCodeEnum.DELIVERY_TYPE_ERROR);
        }

        // 校验订单费用信息
        for (CreateOrderParam.OrderAmountParam orderAmountParam : param.getOrderAmountParamList()) {
            if (AmountTypeEnum.getByCode(orderAmountParam.getAmountType()) == null) {
                throw new OrderBizException(OrderErrorCodeEnum.ORDER_AMOUNT_TYPE_PARAM_ERROR);
            }
        }
        Map<Integer, Integer> orderAmountMap = param.getOrderAmountParamList().stream()
                .collect(Collectors.toMap(CreateOrderParam.OrderAmountParam::getAmountType,
                        CreateOrderParam.OrderAmountParam::getAmount));
        // 订单支付原价不能为空
        if (orderAmountMap.get(AmountTypeEnum.ORIGIN_PAY_AMOUNT.getCode()) == null) {
            throw new OrderBizException(OrderErrorCodeEnum.ORDER_ORIGIN_PAY_AMOUNT_IS_NULL);
        }
        // 订单运费不能为空
        if (orderAmountMap.get(AmountTypeEnum.SHIPPING_AMOUNT.getCode()) == null) {
            throw new OrderBizException(OrderErrorCodeEnum.ORDER_SHIPPING_AMOUNT_IS_NULL);
        }
        // 订单实付金额不能为空
        if (orderAmountMap.get(AmountTypeEnum.REAL_PAY_AMOUNT.getCode()) == null) {
            throw new OrderBizException(OrderErrorCodeEnum.ORDER_REAL_PAY_AMOUNT_IS_NULL);
        }
        if (StringUtils.isNotEmpty(param.getCouponId())) {
            // 订单优惠券抵扣金额不能为空
            if (orderAmountMap.get(AmountTypeEnum.COUPON_DISCOUNT_AMOUNT.getCode()) == null) {
                throw new OrderBizException(OrderErrorCodeEnum.ORDER_DISCOUNT_AMOUNT_IS_NULL);
            }
        }
        // 订单支付信息
        Integer totalPayAmount = 0;
        for (CreateOrderParam.OrderPaymentParam paymentParam : param.getOrderPaymentParamList()) {
            if (PayTypeEnum.getByCode(paymentParam.getPayType()) == null) {
                throw new OrderBizException(OrderErrorCodeEnum.PAY_TYPE_PARAM_ERROR);
            }
            if (AccountTypeEnum.getByCode(paymentParam.getAccountType()) == null) {
                throw new OrderBizException(OrderErrorCodeEnum.ACCOUNT_TYPE_PARAM_ERROR);
            }
            totalPayAmount += paymentParam.getPayAmount();
        }

        if (!totalPayAmount.equals(orderAmountMap.get(AmountTypeEnum.REAL_PAY_AMOUNT.getCode()))) {
            throw new OrderBizException(OrderErrorCodeEnum.TOTAL_PAY_AMOUNT_ERROR);
        }

        // 验证订单是否存在
//        OrderInfoDO order = orderInfoDAO.getByOrderId(param.getOrderId());
//        ParamCheckUtil.checkObjectNull(order, OrderErrorCodeEnum.ORDER_EXISTED);
    }
}
